<?php
/*********************************************************************************
 * SugarCRM Community Edition is a customer relationship management program developed by
 * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Affero General Public License version 3 as published by the
 * Free Software Foundation with the addition of the following permission added
 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Affero General Public License along with
 * this program; if not, see http://www.gnu.org/licenses or write to the Free
 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301 USA.
 * 
 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
 * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
 * 
 * The interactive user interfaces in modified source and object code versions
 * of this program must display Appropriate Legal Notices, as required under
 * Section 5 of the GNU Affero General Public License version 3.
 * 
 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
 * these Appropriate Legal Notices must retain the display of the "Powered by
 * SugarCRM" logo. If the display of the logo is not reasonably feasible for
 * technical reasons, the Appropriate Legal Notices must display the words
 * "Powered by SugarCRM".
 ********************************************************************************/

require_once('include/MVC/Controller/SugarController.php');
require_once('custom/modules/Tasks/TaskUtils.php');
require_once('custom/modules/Tasks/FlagUtils.php');

class TasksController extends SugarController {
    
	function action_Accept() {
    	$this->bean->status="In Progress";
    	$this->action_save();
        $this->view = 'detail';
        $id = $this->bean->id;
         $this->bean = new Task();
         $this->bean->retrieve($id);
    }
    function action_AssignCompleted() {
    	global $timedate, $current_user,$db;
    	$new_task = $this->create_task($this->bean->type,"Not Started",$_POST['assigned_user_id'],$_POST['date_due']);
    	$editor = new edanz_Editor();
		$editor->retrieve($new_task->assigned_user_id);
    	if ($editor->isNewEditor()) {
    		$this->create_flag($new_task,"NewEditor");
    	}
    	if ($editor->isOnProbation()) {
    		$this->create_flag($new_task,"ProbationEditor");
    	}
		$this->bean->status ='Completed';
		$this->bean->assigned_user_id = $_POST['assigned_user_id'];
		$this->bean->description = $_POST['description'];
		$this->bean->date_due = $_POST['date_due'];
		$this->change_fee_related($this->bean->id,$new_task->id);
		$this->action_save();
        $this->view = 'detail';
        $id = $this->bean->id;
        $this->bean = new Task();
        $this->bean->retrieve($id);
    }
    function action_DeclineCompleted() {
    	$this->action = 'DetailView';
    	global $db;
    	$users = new User();
	 	$assign_user = $users->get_full_list("","users.user_name='Assignment'",false,0);
    	$new_task = $this->create_task("Reassignment","Not Started",$assign_user);
    	$this->bean->status ='Deferred';
    	$this->bean->description = $_POST['description'];
    	$this->deleted_flag();

		$new_taskid =$new_task->id;
		$taskid =$this->bean->id;
		
    	$query = "UPDATE edanz_fees set edanz_fees.task_id = '$new_taskid' where edanz_fees.task_id = '$taskid'";
    	$db->query($query);
    	
    	$this->action_save();
        $this->view = 'detail';
        
         $id = $this->bean->id;
         $this->bean = new Task();
         $this->bean->retrieve($id);
         
    }
	function action_FastFailCompleted() {
		
		$assigned_user ="";
		$related_deliverable = $this->bean->deliverable_id;
		$tasks = new Task();
		$task_id = $tasks->get_full_list("date_entered desc","tasks.deliverable_id='$related_deliverable'",false,0);
		$taskutils = new TaskUtils();
		foreach ($task_id as $k => $task) {
			if($taskutils->isExpertTask($task)){
				$assigned_user = $task->assigned_user_id;
				break;
			}
		}
		$this->create_task("FastFail","Not Started",$assigned_user);
    	$this->bean->status ='Deferred';
    	$this->bean->description = $_POST['description'];
    	$this->bean->date_due = $_POST['date_due'];
    	//set deliverable fast fail to true
    	$deliverable = new edanz_Deliverables();
        $deliverable->retrieve($this->bean->deliverable_id);
	$deliverable->fast_fail = 0;
	$deliverable->save();
		$this->action_save();
        $this->view = 'detail';
        $id = $this->bean->id;
	    $this->bean = new Task();
	    $this->bean->retrieve($id);
    }
    function action_Clarificationneeded() {
     	//update due date 
		$TaskUtils = new TaskUtils();
		
		if ($TaskUtils->isExpertTask($this->bean)) {
			$deliverable = new edanz_Deliverables();
			$deliverable_current = $deliverable->retrieve($this->bean->deliverable_id);
			$value['deliverable_id']=$deliverable_current->id;
			$document_id = $deliverable_current->retrieve_relationships('edanz_documents_deliverables',$value,'documents_id');
	     	foreach($document_id as $k=>$v) {
	     		$document = new Document();
	     		$document->retrieve($v['documents_id']);
	     		$deliverable = $document->retrieve_relationships('edanz_documents_deliverables',$v,'deliverable_id');
	     		foreach ($deliverable as $k => $deliverable) {
	     			$deliverable_id[] = $deliverable;
	     		}
	     	}
	     	foreach($deliverable_id as $k => $deliverable_ids) {
	     		$deliverables = new edanz_Deliverables();
	     		$deliverable_type = $deliverables->retrieve($deliverable_ids['deliverable_id']);
	     		if($deliverable_type->service == 'ExpertScientificReview') {
		     		$deliverable_current->date_due = $this->bean->date_due ;
		     		break;
	     		}
	     	}
	     	$deliverable_current->save();
		}
		
     	$this->action_save();
     	$this->create_deliverable();

        $this->view = 'detail';
    }
    private function create_task($task_type,$task_status,$assigned_user,$due_date) {
			$task = new Task();
			$task->assigned_user_id = $assigned_user;
			$task->description = $_POST['description'];
			$task->deliverable_id = $this->bean->deliverable_id;
			$task->type = $task_type;
			$task->status = $task_status;
			$task->date_due = $due_date;
			$task_new_name=$this->get_task_new_name($task->deliverable_id);
			$task->name = $task_new_name['job_name']." - ".$task_new_name['deliverable_service']." - ".$task_type;
			$task->save();
			return $task;
	}
	private function get_task_new_name($deliverable_id)
	{
		$task_new_name = array();
		$deliverable =  new edanz_Deliverables();
		$deliverable->retrieve($deliverable_id);
		$task_new_name['deliverable_service']=$deliverable->service;
		$job =  new edanz_Jobs();
		$job->retrieve($deliverable->job_id);
		$task_new_name['job_name']=$job->name;
		return $task_new_name;
	}
    private function change_fee_related($task_id,$new_task_id){
		$tasks = new Task();
		$tasks->retrieve($task_id);
		$task_fee_beans = $tasks->get_linked_beans('fees','edanz_Fees');
		
		if(!empty($task_fee_beans))	{
			foreach ($task_fee_beans as $fee) {
				$fees = new edanz_Fees();
				$fees->retrieve($fee->id);
				$fees->load_relationship('tasks');
				$fees->tasks->delete($fee->id, $task_id);
				$fees->tasks->add($new_task_id);
			}
		}
    }
	private function create_flag($task,$flag_type){
		global $db;
		
		$flag_id = create_guid();
		$deliverable_new_id = $task->deliverable_id;
		
		$flag = new edanz_Flags();
		$flag->name = $flag_type;
		$flag->load_relationship('edanz_deliverables_edanz_flags');
		$flag->edanz_deliverables_edanz_flags->add($flag_id,$deliverable_new_id);
		$flag->save();
		
	}
	private function deleted_flag() {
		global $db;
			$flag = new edanz_Flags();
			$flag_list = $flag->get_full_list();
			$flags_id =array();
			foreach ($flag_list as $list) {
				if ($list->name == "NewEditor" or $list->name == "ProbationEditor")	{
						$flags_id[]= $list->id;
				}				
			}
			if (!empty($flags_id)) {
				foreach ($flags_id as $flag_id) {
					$deliverable_id= $this->bean->deliverable_id;
					$query_delet = "DELETE FROM edanz_deliverables_flags WHERE edanz_deliverables_flags.deliverables_id ='$deliverable_id' and edanz_deliverables_flags.flags_id in ('$flag_id')";
			        $db->query($query_delet);
				}
			}
	}
	private function create_deliverable(){
    	
    	global $db;
    	$deliverable_old = new edanz_Deliverables();
        $deliverable_old->retrieve($this->bean->deliverable_id);
    			$deliverable_new = new edanz_Deliverables();
		    	$deliverable_new->service = "Clarification";
		    	$job =  new edanz_Jobs();
		    	$job_id = $deliverable_old->job_id;
		    	$job = $job->get_full_list("","edanz_jobs.id='$job_id'",false,0);
		    	$deliverable_new->name = $job[0]->name;
		    	$deliverable_new->job_id =$deliverable_old->job_id;
//		    	$deliverable_new->job_name =$list->job_name;
		    	$deliverable_new->save();
		    	$deliverable_new_id=$deliverable_new->id;
		    	$deliverable_old_id=$deliverable_old->id;
		    	$query = "select edanz_documents_deliverables.documents_id from edanz_documents_deliverables where edanz_documents_deliverables.deliverable_id='$deliverable_old_id'";
    			$result_document= $db->query($query);
    			
    			$document_list= array();
				while ($row = $db->fetchByAssoc($result_document)) {
		         $document_list[] = $row;
		        }
		        foreach ($document_list as $document) {
		        	$document_id = $document['documents_id'];
		        	$query_insert="INSERT INTO edanz_documents_deliverables VALUES (UUID(), NOW(), 0, '$document_id', '$deliverable_new_id')";  
					$db->query($query_insert);  
		        }
    }
}

?>
